package com.galengao.activiti;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;

import java.util.List;

/**
 * @version 1.0
 * 2020-07-31 6:41
 */
@Slf4j
public class ActivitiTest {

    private static final ProcessEngine PROCESS_ENGINE = ProcessEngines.getDefaultProcessEngine();
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        // initDataBase25Tables();
        // 使用默认Engine，要求1. 必须有默认的activiti-cfg.xml，且配置类Id必须为processEngineConfiguration
         deployWorkflow();
        startWorkflow();
        TaskService taskService = PROCESS_ENGINE.getTaskService();
        // 获取查询对象
        TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey("Process_1v9cuep");
        // 获取任务
        List<Task> tasks = taskQuery.taskAssignee("zhangsan").list();
        for (Task task : tasks) {
            log.info("--------获取流程实例Id:{}", task.getProcessDefinitionId());
            log.info("--------获取任务Id:{}", task.getId());
            log.info("--------获取任务负责人:{}", task.getAssignee());
            log.info("--------获取任务名称:{}", task.getName());
        }
    }

    /**
     * 启动流程实例
     */
    private static void startWorkflow() {
        RuntimeService runtimeService = PROCESS_ENGINE.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_1v9cuep");
        log.info("--------获取流程部署Id:{}", processInstance.getDeploymentId());
        log.info("--------获取流程定义Id:{}", processInstance.getProcessDefinitionId());
        log.info("--------获取流程实例Id:{}", processInstance.getId());
        log.info("--------获取Id:{}", processInstance.getActivityId());
    }

    /**
     * 部署一个流程实例
     */
    private static void deployWorkflow() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取 RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("diagram/diagram_1.bpmn")
                .addClasspathResource("diagram/bpm1.png").name("请假流程").deploy();
        log.info("--------获取流程名称:{}", deployment.getName());
        log.info("--------获取流程Id:{}", deployment.getId());
        log.info("--------获取流程时间:{}", deployment.getDeploymentTime());
    }

    /**
     * 使用默认单机配置创建工作流相关表
     */
    private static void initDataBase25Tables() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }


}
